package student;

import cn.edu.ljl.DBMS;
import cn.edu.ljl.Student;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;

public class StudentServlet {

    @Test
    public void getAll(){
        String sql="SELECT * FROM grade ORDER BY id DESC";

        try {
            List<Student> students = DBMS.getStudents(sql);
            for (Student s:students){
                System.out.println(s.getName());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    private String search(String key){
        String sql;
        if(key!=null&&!key.isEmpty()){
            sql="SELECT * FROM `grade` WHERE" +
                    " `nam` LIKE '%" + key + "%'" +
                    " OR `subje` LIKE '%" + key + "%'" ;
        } else {
            sql="SELECT * FROM `grade` ORDER BY `id` DESC ";
        }
        return sql;
    }

    @Test
    public void find(){
        String sql=this.search("郑瑞旭");
        List<Student> students;
        try {
            students = DBMS.getStudents(sql);
            for (Student s:students){
                System.out.println(s.getName());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
